import { getRepayments } from '@/api/risk/user';

export default {
  data() {
    return {
      repaymentsAsync: null,

      selectAllStatus: false,

      // 还款
      repayments: {},

      formLoading: false,

      // 结算
      settlement: {
        amount: 0,
        stage: 0,
        orders: []
      }
    };
  },

  created() {
    this.repaymentsAsync = this.getRepayments();
  },

  computed: {
    repaymentDisabled() {
      const { amount, stage } = this.settlement;

      return (amount === 0) || (stage === 0);
    }
  },

  methods: {
    // 全选
    selectAll() {
      this.selectAllStatus = !this.selectAllStatus;
      this.$refs.repaymentsRef.selectAll(this.selectAllStatus);
    },

    // 结算
    handlerSettlement(data) {
      this.settlement = data;
    },

    async getRepayments() {
      try {
        const res = await getRepayments(this.tid);
        this.updateRepaymentsAttr(res.data);

        this.repayments = res.data;

        this.$nextTick(() => {
          this.$refs.repaymentsRef.settlement();
        });

        return res;
      } catch (error) {
        throw new Error(error);
      }
    },

    // 更新data attr
    updateRepaymentsAttr(data) {
      data.new_list.forEach((item) => {
        item.checked = true;
        item.disabled = (item.status === 1);
      });
    }
  }
};
